package xml.uidElements.output;

import java.util.ArrayList;
import java.util.List;

import xml.others.UIDException;
import xml.uidElements.Transition;
import xml.validations.FinderOutputElements;
import xml.validations.StructuresValidator;

public class OutputStructure extends AbstractOutputStructure {

	protected String name;
	protected List<DataOutputElement> listDataItemsAndStructures;
	protected boolean optional;
	private List<Transition> listOutgoingTransitions;

	public OutputStructure(String name, String data) {

		this.name = name;

		if (new StructuresValidator().isValid(data)) {
			listDataItemsAndStructures = new FinderOutputElements().findOutput(data);
		}

	}

	@Override
	public String getName() {
		return name;
	}

	public void addDataOutput(DataOutputElement d) {
		listDataItemsAndStructures.add(d);

	}

	public void setOptional(boolean optional) {
		this.optional = optional;
	}

	public boolean isOptional() {
		return optional;
	}

	public void insertOutgoingTransition(Transition transition) {

		if (listOutgoingTransitions == null)
			listOutgoingTransitions = new ArrayList<Transition>();

		for (int i = 0; i < this.listOutgoingTransitions.size(); i++) {
			Transition trans = this.listOutgoingTransitions.get(i);
			if (trans.getId().equals(transition.getId()))
				throw new UIDException(UIDException.EXISTING_TRANSITION,
						"There is already a transition with this id");
		}
		listOutgoingTransitions.add(transition);

	}
}
